package com.zd.mole.site.mohurd.process;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.task.RequestMethod;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

@Component
public class MohurdDataserviceQueryProjectListHandler implements ProcessHandler {
	
	@Resource
	private TaskService taskService;

	@Override
	public List<Task> handler(Task task, String text) {
		//翻页
		String complexname = RegexUtils.find("\"complexname\":\\[\"(?<complexname>\\d*)\"\\]", text);
		int total = 0;
		int pg = 0;
		int pgsz = 0;
		String regex = "<a sf='pagebar' sf:data=\"\\(\\{pg:(?<pg>\\d+),ps:(?<pgsz>\\d+),tt:(?<total>\\d+),pn:";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(text);
		if(m.find()) {
			total = Integer.parseInt(m.group("total"));
			pg = Integer.parseInt(m.group("pg"));
			pgsz = Integer.parseInt(m.group("pgsz"));
		}
		
		if(total > 450) {
			for (int i = 0; i <= 9; i++) {
				Task newTask = new Task();
				String param = "complexname=" + complexname + i
						+ "&jsxm_region_id="
						+ "&jsxm_region="
						+ "&cons_name="
						+ "&jsxm_name=";
				newTask.setParam(param);
				newTask.setMethod(RequestMethod.POST);
				newTask.setHostUrl(task.getHostUrl());
				newTask.setProcessHandlerClassName(MohurdDataserviceQueryProjectListHandler.class.getSimpleName());
				newTask.setRequestUrl(task.getRequestUrl());
				newTask.setStatus(TaskStatus.Ready);
				newTask.setRemark("total:" + total);
				taskService.save(newTask);
			}
		} else if (pg * pgsz < total) {
			Task newTask = new Task();
			String param = "complexname=" + complexname
					+ "&jsxm_region_id="
					+ "&jsxm_region="
					+ "&cons_name="
					+ "&jsxm_name="
					+ "&$total=" + total
					+ "&$reload=0"
					+ "&$pg=" + (pg + 1)
					+ "&pgsz=" + pgsz;
			newTask.setParam(param);
			newTask.setMethod(RequestMethod.POST);
			newTask.setHostUrl(task.getHostUrl());
			newTask.setProcessHandlerClassName(MohurdDataserviceQueryProjectListHandler.class.getSimpleName());
			newTask.setRequestUrl(task.getRequestUrl());
			newTask.setStatus(TaskStatus.Ready);
			taskService.save(newTask);
			
			//详细页
			String regexReq = "<td data-header=\"项目名称\" style=\"text-align: left;font-size:16px;\"><a target=\"_blank\"" + 
						"\\s+href=\"(/dataservice/query/project/projectDetail/(\\d+))\">";
			Pattern p2 = Pattern.compile(regexReq);
			Matcher m2 = p2.matcher(text);
			while(m2.find()) {
				String requestUrl = m2.group(1);
				Task newTask2 = new Task();
				newTask2.setCode(m2.group(2));
				newTask2.setHostUrl(task.getHostUrl());
				newTask2.setRequestUrl(requestUrl);
				newTask2.setMethod(RequestMethod.GET);
				newTask2.setProcessHandlerClassName("MohurdDataserviceQueryProjectProjectDetailHandler");
				newTask.setStatus(TaskStatus.Ready);
				taskService.save(newTask2);
			}
		}
		return null;
	}

}
